<template>
  <div>
    <el-container>
      <el-header style="height:auto;">
        <el-input
          v-model="kwInput"
          placeholder="输入广告ID搜索"
          style="width: 40%;float: right"
          clearable
          class=""
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" style="width: 180px" type="success" icon="el-icon-search" @click="handleSearch">搜索
          </el-button>
        </el-input>
        <el-date-picker
          v-model="timerange"
          style="float: right;margin-right: 10px"
          type="datetimerange"
          value-format="timestamp"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-header>
      <el-main>
        <el-table
          v-loading.fullscreen.lock="listLoading"
          :data="data"
          border
          fit
          highlight-current-row
          style="width: 100%"
          show-summary
          @sort-change="handleSort"
        >
          <el-table-column align="center" label="网站" prop="website__domain" sortable="custom" />
          <el-table-column align="center" label="广告ID" prop="ads_id" sortable="custom" />
          <el-table-column align="center" label="充值金额" prop="amount" sortable="custom" />
          <el-table-column align="center" label="备注" prop="note" sortable="custom" />
          <el-table-column align="center" label="支付日期" prop="create_at" sortable="custom">
            <template slot-scope="scope">
              <i class="el-icon-date">{{ scope.row.create_at.split('T')[0] }}</i>
              <i class="el-icon-time">{{ scope.row.create_at.split('T')[1] }}</i>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="400">
            <template slot-scope="scope">
              <el-button size="mini" type="success" @click="handleAction('edit', scope.row)">修改</el-button>
              <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>

        <el-pagination
          v-if="data.length"
          background
          class="mt-pagination"
          :current-page="page"
          :page-sizes="[100,500,1000,5000]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
        <el-dialog
          append-to-body
          :title="title"
          :width="width"
          :visible.sync="innerDialogVisible"
          :close-on-click-modal="false"
        >
          <el-form :model="temp" label-position="left" label-width="80px">

            <el-form-item v-if="action !== 'delete'" label="ads">
              <el-input v-model="temp.ads_id" />
            </el-form-item>
            <el-form-item v-if="action !== 'delete'" label="充值金额(RMB)">
              <el-input v-model="temp.amount" />
            </el-form-item>
            <el-form-item v-if="action !== 'delete'" label="备注">
              <el-input v-model="temp.note" />
            </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="innerDialogVisible = false">取 消</el-button>
            <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
          </span>
        </el-dialog>
      </el-main>

    </el-container>

  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/orders/adwordsrecharges'
import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Adsrecharge',
  data() {
    return {
      timerange: '',
      data: [],
      listLoading: false,
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 1000,
      page: 1,
      total: 0,

      action: null,
      dialogVisible: false,
      title: '广告支付记录',
      temp: {
        amount: '',
        ads_id: this.ads_id,
        note: ''
      },
      width: '',
      innerDialogVisible: false,
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime())
            start.setHours(0, 0, 0)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '昨天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24)
            start.setHours(0, 0, 0)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '最近3天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
            start.setHours(0, 0, 0)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            start.setHours(0, 0, 0)
            end.setTime(end.getTime() - 3600 * 1000 * 24)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '过去14天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 14)
            start.setHours(0, 0, 0)
            end.setTime(end.getTime() - 3600 * 1000 * 24)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            start.setHours(0, 0, 0)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            start.setHours(0, 0, 0)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        website_id: this.website_id,
        ads_id: this.ads_id,
        start: this.timerange ? this.timerange[0] : '',
        end: this.timerange ? this.timerange[1] : '',
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = row
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.title = `确认要删除${row.amount}?`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '50%'
          this.title = `修改${row.amount}`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
      }
    },
    confirm() {
      const _data = this.temp
      switch (this.action) {
        case 'create':
          if (_data.amount === '') {
            alert('amount必填')
            return
          }
          if (_data.ads_id === '') {
            alert('ads_id 必填')
            return
          }
          Post({
            website_id: this.website_id,
            amount: _data.amount,
            ads_id: _data.ads_id,
            note: _data.note
          }
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('删除成功')
          })
          break
        case
          'edit'
          :
          Put(_data.id, {
            website_id: this.website_id,
            ads_id: _data.ads_id,
            amount: _data.amount,
            note: _data.note
          }).then((response) => {
            this.innerDialogVisible = false
            this.$emit('reload')
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.temp = {
        amount: '',
        ads_id: '',
        note: ''
      }
      this.$emit('closeMyDialog', false)
    }
  }
}
</script>

<style scoped>
  .app-container >>> table, tr {
    border-color: black;
  }

  .el-header {
    height: 120px;
    margin-top: 10px;
  }

  .el-header, .el-footer {
    color: white;
  }

  .el-container >>> .pointer {
    cursor: pointer;
    color: #409EFF;
  }

  .el-container >>> a:visited {
    color: purple;
  }

  .el-container >>> a:link {
    color: #409EFF;
  }

  .el-container >>> a:active {
    color: red;
  }

  .app-container >>> a:hover {
    color: red;
    text-decoration: underline;
  }
</style>
